package make

import (
	"fmt"
	"github.com/spf13/cobra"
	"os"
)

var CmdMakeModel = &cobra.Command{
	Use: "model",
	Short: "Create model file, such as : make model user",
	Run: func(cmd *cobra.Command, args []string) {

		// 格式化模型名称，返回一个model对象
		model := makeModelFromString(args[0])

		// 确保模型的目录存在，例如：'app/models/user'
		dir := fmt.Sprintf("app/models/%s/", model.PackageName)
		// os.MkdirAll 会确保父目录和子目录都会创建，第二个参数是目录权限，使用 0777
		_ = os.MkdirAll(dir, os.ModePerm)

		// 替换变量
		createFileFromStub(dir + model.PackageName + "_model.go", "model/model", model)
		createFileFromStub(dir + model.PackageName + "_util.go", "model/model_util", model)
		createFileFromStub(dir + model.PackageName + "_hooks.go", "model/model_hooks", model)
	},
	Args: cobra.ExactArgs(1), // 只允许且必须传 1 个参数
}
